using Mapster;
using MapsterMapper;
using RuoVea.ExDto;
using RuoVea.ExSugar.Entity;
using RuoVea.ExSugar.Option;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace RuoVea.ExSugar;

/// <summary>
/// SqlSugar 仓储实现类
/// </summary>
public static class SqlSugarExtensions
{
    #region 判断类型是否实现某个泛型
    /// <summary>
    /// 判断类型是否实现某个泛型
    /// </summary>
    /// <param name="type">类型</param>
    /// <param name="generic">泛型类型</param>
    /// <returns>bool</returns>
    public static bool HasImplementedRawGeneric(this Type type, Type generic)
    {
        // 检查接口类型
        var isTheRawGenericType = type.GetInterfaces().Any(IsTheRawGenericType);
        if (isTheRawGenericType) return true;

        // 检查类型
        while (type != null && type != typeof(object))
        {
            isTheRawGenericType = IsTheRawGenericType(type);
            if (isTheRawGenericType) return true;
            type = type.BaseType;
        }

        return false;

        // 判断逻辑
        bool IsTheRawGenericType(Type type) => generic == (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
    }
    #endregion

    #region Count
    /// <summary>
    /// 获取总数
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static int Count<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().Count(whereExpression);
    }

    /// <summary>
    /// 获取总数
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static Task<int> CountAsync<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().CountAsync(whereExpression);
    }
    #endregion

    #region Any
    /// <summary>
    /// 检查是否存在
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static bool Any<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().Any(whereExpression);
    }

    /// <summary>
    /// 检查是否存在
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static async Task<bool> AnyAsync<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return await db.Queryable<TEntity>().AnyAsync(whereExpression);
    }
    #endregion

    #region Single
    /// <summary>
    /// 通过主键获取实体
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    public static TEntity Single<TEntity>(this SqlSugarClient db, dynamic Id) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().InSingle(Id);
    }

    /// <summary>
    /// 获取一个实体
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static TEntity Single<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().Single(whereExpression);
    }

    /// <summary>
    /// 获取一个实体
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static Task<TEntity> SingleAsync<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().SingleAsync(whereExpression);
    }
    #endregion

    #region FirstOrDefault
    /// <summary>
    /// 获取一个实体
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static TEntity FirstOrDefault<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().First(whereExpression);
    }

    /// <summary>
    /// 获取一个实体
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static async Task<TEntity> FirstOrDefaultAsync<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return await db.Queryable<TEntity>().FirstAsync(whereExpression);
    }
    #endregion

    #region ToList
    /// <summary>
    /// 获取列表
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static List<TEntity> ToList<TEntity>(this SqlSugarClient db) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().ToList();
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static List<TEntity> ToList<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().Where(whereExpression).ToList();
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <param name="orderByExpression"></param>
    /// <param name="orderByType"></param>
    /// <returns></returns>
    public static List<TEntity> ToList<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToList();
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static Task<List<TEntity>> ToListAsync<TEntity>(this SqlSugarClient db) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().ToListAsync();
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static Task<List<TEntity>> ToListAsync<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().Where(whereExpression).ToListAsync();
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <param name="orderByExpression"></param>
    /// <param name="orderByType"></param>
    /// <returns></returns>
    public static Task<List<TEntity>> ToListAsync<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToListAsync();
    }
    #endregion

    #region Insert
    /// <summary>
    /// 新增一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static int Insert<TEntity>(this SqlSugarClient db, TEntity entity, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Insertable(entity).IgnoreColumns(IgnoreColumns).ExecuteCommand();
    }

    /// <summary>
    /// 新增多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="IgnoreColumns"></param>
    /// <param name="entities"></param>
    /// <returns></returns>
    public static int Insert<TEntity>(this SqlSugarClient db, bool IgnoreColumns = true, params TEntity[] entities) where TEntity : class, new()
    {
        return db.Insertable(entities).IgnoreColumns(IgnoreColumns).ExecuteCommand();
    }

    /// <summary>
    /// 新增多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entities"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static int Insert<TEntity>(this SqlSugarClient db, IEnumerable<TEntity> entities, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Insertable(entities.ToArray()).IgnoreColumns(IgnoreColumns).ExecuteCommand();
    }

    /// <summary>
    /// 新增一条记录返回自增Id
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static int InsertReturnIdentity<TEntity>(this SqlSugarClient db, TEntity insertObj, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Insertable(insertObj).IgnoreColumns(IgnoreColumns).ExecuteReturnIdentity();
    }

    /// <summary>
    /// 新增一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static Task<int> InsertAsync<TEntity>(this SqlSugarClient db, TEntity entity, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Insertable(entity).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 新增多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="IgnoreColumns"></param>
    /// <param name="entities"></param>
    /// <returns></returns>
    public static Task<int> InsertAsync<TEntity>(this SqlSugarClient db, bool IgnoreColumns = true, params TEntity[] entities) where TEntity : class, new()
    {
        return db.Insertable(entities).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 新增多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entities"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static Task<int> InsertAsync<TEntity>(this SqlSugarClient db, IEnumerable<TEntity> entities, bool IgnoreColumns = true) where TEntity : class, new()
    {
        if (entities != null && entities.Any())
        {
            return db.Insertable(entities.ToArray()).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
        }
        return Task.FromResult(0);
    }

    /// <summary>
    /// 新增一条记录返回自增Id
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static async Task<long> InsertReturnIdentityAsync<TEntity>(this SqlSugarClient db, TEntity entity, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return await db.Insertable(entity).IgnoreColumns(IgnoreColumns).ExecuteReturnBigIdentityAsync();
    }
    #endregion

    #region Update
    /// <summary>
    /// 更新一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static int Update<TEntity>(this SqlSugarClient db, TEntity entity, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Updateable(entity).IgnoreColumns(IgnoreColumns).ExecuteCommand();
    }

    /// <summary>
    /// 更新多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="IgnoreColumns"></param>
    /// <param name="entities"></param>
    /// <returns></returns>
    public static int Update<TEntity>(this SqlSugarClient db, bool IgnoreColumns = true, params TEntity[] entities) where TEntity : class, new()
    {
        return db.Updateable(entities).IgnoreColumns(IgnoreColumns).ExecuteCommand();
    }

    /// <summary>
    /// 更新多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entities"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static int Update<TEntity>(this SqlSugarClient db, IEnumerable<TEntity> entities, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Updateable(entities.ToArray()).IgnoreColumns(IgnoreColumns).ExecuteCommand();
    }

    /// <summary>
    /// 更新一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static Task<int> UpdateAsync<TEntity>(this SqlSugarClient db, TEntity entity, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Updateable(entity).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 无主键更新一条记录
    /// </summary>    
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity">更新的实体</param>
    /// <param name="columns">根据那些字段更新</param>
    /// <returns></returns>
    public static Task<int> UpdateNoPrimaryKey<TEntity>(this SqlSugarClient db, TEntity entity, Expression<Func<TEntity, object>> columns, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Updateable(entity).WhereColumns(columns).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 无主键更新一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity">更新的实体</param>
    /// <param name="columns">根据那些字段更新</param>
    /// <returns></returns>
    public static Task<int> UpdateNoPrimaryKeyAsync<TEntity>(this SqlSugarClient db, TEntity entity, Expression<Func<TEntity, object>> columns, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Updateable(entity).WhereColumns(columns).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 无主键更新多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entitys">更新的实体</param>
    /// <param name="columns">根据那些字段更新</param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static Task<int> UpdateNoPrimaryKey<TEntity>(this SqlSugarClient db, List<TEntity> entitys, Expression<Func<TEntity, object>> columns, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Updateable(entitys).WhereColumns(columns).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 无主键更新多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entitys">更新的实体</param>
    /// <param name="columns">根据那些字段更新</param> 
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static Task<int> UpdateNoPrimaryKeyAsync<TEntity>(this SqlSugarClient db, List<TEntity> entitys, Expression<Func<TEntity, object>> columns, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Updateable(entitys).WhereColumns(columns).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 更新多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="IgnoreColumns"></param>
    /// <param name="entities"></param>
    /// <returns></returns>
    public static Task<int> UpdateAsync<TEntity>(this SqlSugarClient db, bool IgnoreColumns = true, params TEntity[] entities) where TEntity : class, new()
    {
        return db.Updateable(entities).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 更新多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entities"></param>
    /// <param name="IgnoreColumns"></param>
    /// <returns></returns>
    public static Task<int> UpdateAsync<TEntity>(this SqlSugarClient db, IEnumerable<TEntity> entities, bool IgnoreColumns = true) where TEntity : class, new()
    {
        return db.Updateable(entities.ToArray()).IgnoreColumns(IgnoreColumns).ExecuteCommandAsync();
    }
    #endregion

    #region Delete
    /// <summary>
    /// 删除一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static int Delete<TEntity>(this SqlSugarClient db, TEntity entity) where TEntity : class, new()
    {
        return db.Deleteable(entity).ExecuteCommand();
    }

    /// <summary>
    /// 删除一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static int Delete<TEntity>(this SqlSugarClient db, object key) where TEntity : class, new()
    {
        return db.Deleteable<TEntity>().In(key).ExecuteCommand();
    }

    /// <summary>
    /// 删除多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="keys"></param>
    /// <returns></returns>
    public static int Delete<TEntity>(this SqlSugarClient db, params object[] keys) where TEntity : class, new()
    {
        return db.Deleteable<TEntity>().In(keys).ExecuteCommand();
    }

    /// <summary>
    /// 自定义条件删除记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static int Delete<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Deleteable<TEntity>().Where(whereExpression).ExecuteCommand();
    }

    /// <summary>
    /// 删除一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static Task<int> DeleteAsync<TEntity>(this SqlSugarClient db, TEntity entity) where TEntity : class, new()
    {
        return db.Deleteable(entity).ExecuteCommandAsync();
    }

    /// <summary>
    /// 删除一条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static Task<int> DeleteAsync<TEntity>(this SqlSugarClient db, object key) where TEntity : class, new()
    {
        return db.Deleteable<TEntity>().In(key).ExecuteCommandAsync();
    }

    /// <summary>
    /// 删除多条记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="keys"></param>
    /// <returns></returns>
    public static Task<int> DeleteAsync<TEntity>(this SqlSugarClient db, params object[] keys) where TEntity : class, new()
    {
        return db.Deleteable<TEntity>().In(keys).ExecuteCommandAsync();
    }

    /// <summary>
    /// 自定义条件删除记录
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static async Task<int> DeleteAsync<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return await db.Deleteable<TEntity>().Where(whereExpression).ExecuteCommandAsync();
    }
    #endregion

    #region AsQueryable
    /// <summary>
    /// 构建查询分析器
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static ISugarQueryable<TEntity> AsQueryable<TEntity>(this SqlSugarClient db) where TEntity : class, new()
    {
        return db.Queryable<TEntity>();
    }

    /// <summary>
    /// 构建查询分析器
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="predicate"></param>
    /// <returns></returns>
    public static ISugarQueryable<TEntity> AsQueryable<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> predicate) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().Where(predicate);
    }

    /// <summary>
    /// 直接返回数据库结果
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static List<TEntity> AsEnumerable<TEntity>(this SqlSugarClient db) where TEntity : class, new()
    {
        return db.AsQueryable<TEntity>().ToList();
    }

    /// <summary>
    /// 直接返回数据库结果
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="predicate"></param>
    /// <returns></returns>
    public static List<TEntity> AsEnumerable<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> predicate) where TEntity : class, new()
    {
        return db.AsQueryable(predicate).ToList();
    }

    /// <summary>
    /// 直接返回数据库结果
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static Task<List<TEntity>> AsAsyncEnumerable<TEntity>(this SqlSugarClient db) where TEntity : class, new()
    {
        return db.AsQueryable<TEntity>().ToListAsync();
    }

    /// <summary>
    /// 直接返回数据库结果
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="predicate"></param>
    /// <returns></returns>
    public static Task<List<TEntity>> AsAsyncEnumerable<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> predicate) where TEntity : class, new()
    {
        return db.AsQueryable(predicate).ToListAsync();
    }
    #endregion

    #region IsExists
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static bool IsExists<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().Any(whereExpression);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static Task<bool> IsExistsAsync<TEntity>(this SqlSugarClient db, Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return db.Queryable<TEntity>().AnyAsync(whereExpression);
    }
    #endregion




    /// <summary>
    /// 实体假删除 _rep.FakeDelete(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repository"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static int FakeDelete<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
    {
        return repository.Context.FakeDelete(entity);
    }

    /// <summary>
    /// 实体假删除 db.FakeDelete(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static int FakeDelete<T>(this ISqlSugarClient db, T entity) where T : EntityBase, new()
    {
        return db.Updateable(entity).ReSetValue(x => { x.IsDeleted = ExEnum.IsDelete.Y; })
            .IgnoreColumns(ignoreAllNullColumns: true)
            .EnableDiffLogEvent()   // 记录差异日志
            .UpdateColumns(x => new { x.IsDeleted, x.ModifyTime, x.Modifier })  // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
            .ExecuteCommand();
    }

    /// <summary>
    /// 实体假删除异步 _rep.FakeDeleteAsync(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repository"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static Task<int> FakeDeleteAsync<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
    {
        return repository.Context.FakeDeleteAsync(entity);
    }

    /// <summary>
    /// 实体假删除 db.FakeDelete(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static Task<int> FakeDeleteAsync<T>(this ISqlSugarClient db, T entity) where T : EntityBase, new()
    {
        return db.Updateable(entity).ReSetValue(x => { x.IsDeleted = ExEnum.IsDelete.Y; })
            .IgnoreColumns(ignoreAllNullColumns: true)
            .EnableDiffLogEvent()   // 记录差异日志
            .UpdateColumns(x => new { x.IsDeleted, x.ModifyTime, x.Modifier })  // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
            .ExecuteCommandAsync();
    }

    /// <summary>
    /// 排序方式(默认降序)
    /// </summary>
    /// <param name="queryable"></param>
    /// <param name="pageInput"> </param>
    /// <param name="defualtSortField"> 默认排序字段 </param>
    /// <param name="descSort"> 是否降序 </param>
    /// <returns> </returns>
    public static ISugarQueryable<T> OrderBuilder<T, Input>(this ISugarQueryable<T> queryable, Input pageInput, string defualtSortField = "Id", bool descSort = true) where Input : PageParam,new()
    {
        var orderStr = "";
        // 约定默认每张表都有Id排序
        if (!string.IsNullOrWhiteSpace(defualtSortField))
        {
            orderStr = descSort ? defualtSortField + " Desc" : defualtSortField + " Asc";
        }
        TypeAdapterConfig typeAdapterConfig = new();
        typeAdapterConfig.ForType<T, Input>().IgnoreNullValues(true);
        Mapper mapper = new(typeAdapterConfig); // 务必将mapper设为单实例
        var nowPagerInput = mapper.Map<Input>(pageInput);

        // 排序是否可用-排序字段和排序顺序都为非空才启用排序
        if (!string.IsNullOrEmpty(nowPagerInput.Sidx) && !string.IsNullOrEmpty(nowPagerInput.Sord))
        {
            orderStr = $"{nowPagerInput.Sidx} {(nowPagerInput.Sord == "Desc" ? "Desc" : "Asc")}";
        }
        return queryable.OrderByIF(!string.IsNullOrWhiteSpace(orderStr), orderStr);
    }

    /// <summary>
    /// 更新实体并记录差异日志 _rep.UpdateWithDiffLog(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repository"></param>
    /// <param name="entity"></param>
    /// <param name="ignoreAllNullColumns"></param>
    /// <returns></returns>
    public static int UpdateWithDiffLog<T>(this ISugarRepository repository, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
    {
        return repository.Context.UpdateWithDiffLog(entity, ignoreAllNullColumns);
    }

    /// <summary>
    /// 更新实体并记录差异日志 _rep.UpdateWithDiffLog(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <param name="ignoreAllNullColumns"></param>
    /// <returns></returns>
    public static int UpdateWithDiffLog<T>(this ISqlSugarClient db, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
    {
        return db.Updateable(entity)
            .IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns)
            .EnableDiffLogEvent()
            .ExecuteCommand();
    }

    /// <summary>
    /// 更新实体并记录差异日志 _rep.UpdateWithDiffLogAsync(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repository"></param>
    /// <param name="entity"></param>
    /// <param name="ignoreAllNullColumns"></param>
    /// <returns></returns>
    public static Task<int> UpdateWithDiffLogAsync<T>(this ISugarRepository repository, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
    {
        return repository.Context.UpdateWithDiffLogAsync(entity, ignoreAllNullColumns);
    }

    /// <summary>
    /// 更新实体并记录差异日志 _rep.UpdateWithDiffLogAsync(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <param name="ignoreAllNullColumns"></param>
    /// <returns></returns>
    public static Task<int> UpdateWithDiffLogAsync<T>(this ISqlSugarClient db, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
    {
        return db.Updateable(entity)
            .IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns)
            .EnableDiffLogEvent()
            .ExecuteCommandAsync();
    }

    /// <summary>
    /// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repository"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static int InsertWithDiffLog<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
    {
        return repository.Context.InsertWithDiffLog(entity);
    }

    /// <summary>
    /// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static int InsertWithDiffLog<T>(this ISqlSugarClient db, T entity) where T : EntityBase, new()
    {
        return db.Insertable(entity).EnableDiffLogEvent().ExecuteCommand();
    }

    /// <summary>
    /// 新增实体并记录差异日志 _rep.InsertWithDiffLogAsync(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repository"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static Task<int> InsertWithDiffLogAsync<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
    {
        return repository.Context.InsertWithDiffLogAsync(entity);
    }

    /// <summary>
    /// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static Task<int> InsertWithDiffLogAsync<T>(this ISqlSugarClient db, T entity) where T : EntityBase, new()
    {
        return db.Insertable(entity).EnableDiffLogEvent().ExecuteCommandAsync();
    }



}